package com.ruoyi.model.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.ModelPlanAlgorithm;
import com.ruoyi.model.domain.ModelTaskProcessParam;
import com.ruoyi.model.domain.vo.ModelPlanAlgorithmVo;
import com.ruoyi.model.domain.vo.ModelPlanVo;
import com.ruoyi.model.domain.vo.ModelTaskProcessParamVo;
import com.ruoyi.model.service.IModelPlanAlgorithmService;
import com.ruoyi.model.service.IModelTaskProcessParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.ModelPlanMapper;
import com.ruoyi.model.domain.ModelPlan;
import com.ruoyi.model.service.IModelPlanService;

/**
 * 模型方案Service业务层处理
 *
 * @author rww
 * @date 2024-11-25
 */
@Service
public class ModelPlanServiceImpl implements IModelPlanService
{
    @Autowired
    private ModelPlanMapper modelPlanMapper;

    @Autowired
    private IModelTaskProcessParamService modelTaskProcessParamService;
    @Autowired
    private IModelPlanAlgorithmService modelPlanAlgorithmService;

    /**
     * 查询模型方案
     *
     * @param id 模型方案主键
     * @return 模型方案
     */
    @Override
    public ModelPlan selectModelPlanById(Long id)
    {
        return modelPlanMapper.selectModelPlanById(id);
    }

    @Override
    public ModelPlanVo selectModelPlanVoById(Long id) {
        ModelPlan info=selectModelPlanById(id);
        ModelPlanVo planVo= BeanUtil.copyProperties(info,ModelPlanVo.class);
        if(ObjectUtil.isNotEmpty(planVo)){
            ModelPlanAlgorithm algorithm=new ModelPlanAlgorithm();
            algorithm.setPlanId(planVo.getId());
            List<ModelPlanAlgorithmVo> algorithmList=modelPlanAlgorithmService.selectModelPlanAlgorithmList(algorithm);
            if(ObjectUtil.isNotEmpty(algorithmList)) {
                for (ModelPlanAlgorithmVo planAlgorithm : algorithmList) {
                    ModelTaskProcessParam param = new ModelTaskProcessParam();
                    param.setAlgorithmId(planAlgorithm.getAlgorithmId());
                    param.setParamType("1");
                    param.setPlanId(id);
                    List<ModelTaskProcessParamVo> paramList = modelTaskProcessParamService.selectModelTaskProcessParamList(param);
                    planAlgorithm.setParamList(paramList);
                }
                planVo.setAlgorithmList(algorithmList);
            }
        }
        return planVo;
    }

    /**
     * 查询模型方案列表
     *
     * @param modelPlan 模型方案
     * @return 模型方案
     */
    @Override
    public List<ModelPlan> selectModelPlanList(ModelPlan modelPlan)
    {
        return modelPlanMapper.selectModelPlanList(modelPlan);
    }

    /**
     * 新增模型方案
     *
     * @param modelPlan 模型方案
     * @return 结果
     */
    @Override
    public int insertModelPlan(ModelPlanVo modelPlan)
    {
            modelPlan.setId(IdUtil.getSnowflakeNextId());
            modelPlan.setCreateTime(DateUtils.getNowDate());
            int num=modelPlanMapper.insertModelPlan(modelPlan);
            if(num>0) {
            for (ModelPlanAlgorithmVo algorithmVo : modelPlan.getAlgorithmList()) {
                algorithmVo.setPlanId(modelPlan.getId());
                modelPlanAlgorithmService.insertModelPlanAlgorithm(algorithmVo);
                for (ModelTaskProcessParamVo paramVo:algorithmVo.getParamList()){
                    paramVo.setParamType("1");
                    paramVo.setPlanId(modelPlan.getId());
                    modelTaskProcessParamService.insertModelTaskProcessParam(paramVo);
                }
            }
        }

        return num;
    }

    /**
     * 修改模型方案
     *
     * @param modelPlan 模型方案
     * @return 结果
     */
    @Override
    public int updateModelPlan(ModelPlan modelPlan)
    {
        modelPlan.setUpdateTime(DateUtils.getNowDate());
        return modelPlanMapper.updateModelPlan(modelPlan);
    }

    @Override
    public int updateModelPlanVo(ModelPlanVo modelPlan) {
        int num=updateModelPlan(modelPlan);
        if(num>0){
            deleteAlgorithm(modelPlan.getId());
            for (ModelPlanAlgorithmVo algorithmVo : modelPlan.getAlgorithmList()) {
                algorithmVo.setPlanId(modelPlan.getId());
                modelPlanAlgorithmService.insertModelPlanAlgorithm(algorithmVo);
                for (ModelTaskProcessParamVo paramVo:algorithmVo.getParamList()){
                    paramVo.setParamType("1");
                    paramVo.setPlanId(modelPlan.getId());
                    modelTaskProcessParamService.insertModelTaskProcessParam(paramVo);
                }
            }
        }
        return num;
    }

    private void deleteAlgorithm(Long id){
        ModelPlanAlgorithm algorithm=new ModelPlanAlgorithm();
        algorithm.setPlanId(id);
        List<ModelPlanAlgorithmVo> algorithmList=modelPlanAlgorithmService.selectModelPlanAlgorithmList(algorithm);
        if(ObjectUtil.isNotEmpty(algorithmList)) {
            for (ModelPlanAlgorithmVo planAlgorithm : algorithmList) {
                ModelTaskProcessParam param = new ModelTaskProcessParam();
                param.setAlgorithmId(planAlgorithm.getAlgorithmId());
                param.setParamType("1");
                param.setPlanId(id);
                List<ModelTaskProcessParamVo> paramList = modelTaskProcessParamService.selectModelTaskProcessParamList(param);
                if (ObjectUtil.isNotEmpty(paramList)) {
                    Long[] ids=  paramList.stream().map(ModelTaskProcessParam::getId).toArray(Long[]::new);
                    modelTaskProcessParamService.deleteModelTaskProcessParamByIds(ids);
                }
                modelPlanAlgorithmService.deleteModelPlanAlgorithmById(planAlgorithm.getId());
            }
        }
    }

    /**
     * 批量删除模型方案
     *
     * @param ids 需要删除的模型方案主键
     * @return 结果
     */
    @Override
    public int deleteModelPlanByIds(Long[] ids)
    {
        return modelPlanMapper.deleteModelPlanByIds(ids);
    }

    /**
     * 删除模型方案信息
     *
     * @param id 模型方案主键
     * @return 结果
     */
    @Override
    public int deleteModelPlanById(Long id)
    {

        deleteAlgorithm(id);
        return modelPlanMapper.deleteModelPlanById(id);
    }
}
